<template>
  <div>
    <el-dialog
      title="新增员工"
      :visible="showDialog"
      @close="btnCancel"
    >
      <el-form ref="formData" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="formData.username" style="width:80%" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="formData.mobile" style="width:80%" placeholder="请输入手机" />
        </el-form-item>
        <el-form-item label="入职时间" prop="timeOfEntry">
          <el-input v-model="formData.timeOfEntry" style="width:80%" placeholder="入职时间" />
        </el-form-item>
        <el-form-item label="聘用形式" prop="formOfEmployment">
          <el-select v-model="formData.formOfEmployment" style="width:80%" placeholder="聘用形式">
            <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="工号" prop="workNumber">
          <el-input v-model="formData.workNumber" style="width:80%" placeholder="工号" />
        </el-form-item>
        <el-form-item label="部门" prop="departmentName">
          <el-input v-model="formData.departmentName" style="width:80%" placeholder="部门" @focus="getDepartments" />
          <el-tree
            v-if="showTree"
            loading="true"
            class="trees"
            :data="treeData"
            :props="{ label:'name'}"
            :default-expand-all="true"
            @node-click="selectNode"
          />
        </el-form-item>
        <el-form-item label="转正时间" prop="correctionTime">
          <el-date-picker v-model="formData.correctionTime" style="width:80%" placeholder="转正时间" />
        </el-form-item>
      </el-form>
      <template v-slot:footer>
        <el-row type="flex" justify="center">
          <el-col :span="6">
            <el-button @click="btnCancel">取 消</el-button>
            <el-button type="primary" @click="btnOK">确 定</el-button>
          </el-col>
        </el-row>

      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getRequiredRules } from '@/utils/validate'
import { getDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils'
import EmployeeEnum from '@/api/constant/employees'
import { addEmployee } from '@/api/employees'
export default {
  name: '',
  components: {},
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }

  },
  data() {
    return {
      EmployeeEnum,
      treeData: [],
      showTree: false,
      loading: false,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          getRequiredRules('用户名'),
          {
            min: 1, max: 5, message: '用户名位数不符合规则', trigger: 'blur'
          }
        ],
        mobile: [
          getRequiredRules('手机号'),
          {
            pattern: /^1[3-9]\d{9}$/, message: '手机号位数不符合规则', trigger: 'blur'
          }
        ],
        formOfEmployment: [getRequiredRules('聘用形式')],
        workNumber: [getRequiredRules('工号')],
        departmentName: [{ required: true, trigger: 'change', message: '部门名称不能为空' }],
        timeOfEntry: [getRequiredRules('入职时间')],
        correctionTime: [getRequiredRules('转正时间')]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    // this.getDepartments()
  },
  methods: {
    selectNode(node) {
      this.formData.departmentName = node.name // 将选中的节点赋值给表单
      this.showTree = false // 关闭弹层
    },
    async getDepartments() {
      // 树 打开
      this.showTree = true
      // 调用数据提前加载
      this.loading = true
      // 获取部门所有数据
      const { depts } = await getDepartments()
      // 数组转树结构
      this.treeData = tranListToTreeData(depts, '')
      // 关闭loading
      this.loading = false
    },
    async btnOK() {
      try {
        // 校验表单
        await this.$refs.formData.validate()
        // 提交数据
        await addEmployee(this.formData)
        // 提示成功
        this.$message.success('添加成功')
        // 告诉父组件更新数据
        // this.$parent 可以直接调用到父组件的实例 实际上就是父组件this
        // this.$emit
        this.$parent.getEmployeeList()
        this.$parent.showDialog = false
      } catch (err) {
        console.log(err)
      }
    },
    btnCancel() {
      // 表单置空
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      // 校验重置
      this.$refs.formData.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style scoped lang="scss">
.trees{
  position: absolute;
  border: 1px solid #ccc;
  width: 80%;
  border-radius: 5px;
  z-index: 99;
  margin-top: 5px;
  height: 300px;
  overflow: scroll;
}
</style>
